/*
 * Created on 2006年9月8日
 *
 */
package com.raymond.dp;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.MemoryImageSource;

import javax.swing.JOptionPane;

public class DeadPixelsTester {

    private static final Image image = Toolkit.getDefaultToolkit().createImage(
            new MemoryImageSource(16, 16, new int[16 * 16], 0, 16));

    private static final Color[] COLORS = new Color[] { Color.black,
            Color.white, Color.red, Color.green, Color.blue };

    private Frame mainFrame;

    private boolean isExist = false;

    private int index = 0;

    public DeadPixelsTester() {
        GraphicsEnvironment env = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();
        try {
            Cursor transparentCursor = Toolkit.getDefaultToolkit()
                    .createCustomCursor(image, new Point(0, 0),
                            "invisiblecursor");
            GraphicsConfiguration gc = device.getDefaultConfiguration();
            mainFrame = new Frame(gc);
            mainFrame.setUndecorated(true);
            mainFrame.setIgnoreRepaint(true);
            mainFrame.addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e) {
                    int code = e.getKeyCode();
                    switch (code) {
                    case KeyEvent.VK_ESCAPE:
                        isExist = true;
                        break;
                    case KeyEvent.VK_SPACE:
                        index++;
                        break;
                    case KeyEvent.VK_BACK_SPACE:
                        if (index > 0) {
                            index--;
                        }
                        break;
                    default:
                        break;
                    }
                }
            });
            device.setFullScreenWindow(mainFrame);
            mainFrame.setCursor(transparentCursor);
            Rectangle bounds = mainFrame.getBounds();
            mainFrame.createBufferStrategy(2);
            BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();
            do {
                if (index < COLORS.length) {
                    if (!bufferStrategy.contentsLost()) {
                        Graphics g = bufferStrategy.getDrawGraphics();
                        try {
                            g.setColor(COLORS[index]);
                            g.fillRect(0, 0, bounds.width, bounds.height);
                        } finally {
                            g.dispose();
                        }
                        bufferStrategy.show();
                    }
                } else {
                    isExist = true;
                }
            } while (!isExist);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(),
                    "Interanl Error", JOptionPane.ERROR_MESSAGE);
        } finally {
            mainFrame.setCursor(Cursor.getDefaultCursor());
            device.setFullScreenWindow(null);
        }
        mainFrame.dispose();
        mainFrame = null;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            new DeadPixelsTester();
        } catch (HeadlessException he) {
            System.err.println(he.getMessage());
        }
        System.exit(0);
    }

}
